package click
import grails.plugins.springsecurity.Secured


@Secured(['ROLE_ADMIN'])
class WebActionController {

    static allowedMethods = [save: "POST", update: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 20, 100)
        [webActionInstanceList: WebAction.list(params), webActionInstanceTotal: WebAction.count()]
    }

    def create = {
        def webActionInstance = new WebAction()
        webActionInstance.properties = params
        return [webActionInstance: webActionInstance]
    }

    def save = {
        def webActionInstance = new WebAction(params)
        if (webActionInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'webAction.label', default: 'WebAction'), webActionInstance])}"
            redirect(action: "list")
        }
        else {
            render(view: "create", model: [webActionInstance: webActionInstance])
        }
    }

    def show = {
        def webActionInstance = WebAction.get(params.id)
        if (!webActionInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'webAction.label', default: 'WebAction'), params.id])}"
            redirect(action: "list")
        }
        else {
            [webActionInstance: webActionInstance]
        }
    }

    def edit = {
        def webActionInstance = WebAction.get(params.id)
        if (!webActionInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'webAction.label', default: 'WebAction'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [webActionInstance: webActionInstance]
        }
    }

    def update = {
        def webActionInstance = WebAction.get(params.id)
        if (webActionInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (webActionInstance.version > version) {
                    
                    webActionInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'webAction.label', default: 'WebAction')] as Object[], "Another user has updated this WebAction while you were editing")
                    render(view: "edit", model: [webActionInstance: webActionInstance])
                    return
                }
            }
            webActionInstance.properties = params
            if (!webActionInstance.hasErrors() && webActionInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'webAction.label', default: 'WebAction'), webActionInstance])}"
                redirect(action: "list")
            }
            else {
                render(view: "edit", model: [webActionInstance: webActionInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'webAction.label', default: 'WebAction'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def webActionInstance = WebAction.get(params.id)
        if (webActionInstance) {
            try {
                webActionInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'webAction.label', default: 'WebAction'), webActionInstance])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'webAction.label', default: 'WebAction'), webActionInstance])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'webAction.label', default: 'WebAction'), params.id])}"
            redirect(action: "list")
        }
    }

    def createAll = {
//        def was = ''
//        WebAction.findAllByTask(Task.get(params.taskid) , [sort : 'sortid' , order: 'asc'])?.each{
//            was += "${it.name} , ${it.target}${it.value?' , '+it.value:''}\n"
//        }
       
        return [taskid: params.taskid,  webActionContent : params.content ?params.content : Task.get(params.taskid).buildAction()]
    }

    def saveAll = {
        def task = Task.get(params.taskid)
        if (!task) {
           flash.message = "没有找到taskid，你这个页面是咋整的？？"
            render(view: "createAll", model: ['taskid' : taskid , 'content' : content])
        }
        def webActionList = WebAction.findAllByTask(task) 

//        if (task.actions)  {
//                task.actions.toList().each {
//                        task.actions.remove(it)
//                        //it.delete()
//                } 
//        } 
//        task.save()

        webActionList.each{
           // it.task.actions.remove(it) 
            it.task = null
            it.delete()
        } 

        def webactions = params.content.split("\r\n")
    
        webactions.eachWithIndex{ obj , id->
            def wa = obj.split(",")
            //println "${id}  ${wa[0]} ${wa[1]} ${wa.size() > 2? wa[2] :''}" 
            if (wa.size() > 1) {
                
        
            new WebAction(sortid : id ,
                name: wa[0].trim(),
                target: wa[1].trim(),
                task: task,
                value : wa.size() > 2? wa[2].trim() :""
            ).save();    
                }
            //new WebAction
//   String name = "";
//    String target = "";
//    String value = "";
//    int sortid = 0;        
        }
//        if (webActionInstance.save(flush: true)) {
//
            redirect(action: "list",controller: 'task' , id : params.taskid)
//        }
//        else {
//            render(view: "createAll", model: ['taskid' : taskid , 'content' : content])
//        }
    }

}
